/**
 * 
 */
package com.base.myproject.client.busi.service;

import java.io.Serializable;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.Env;
import com.extjs.gxt.ui.client.data.ModelData;
import com.google.gwt.i18n.client.DateTimeFormat;

/**
 * @author zgw 2009-11-25 下午06:29:47 javazgw@gmail.com
 */
public class CcodeBusiObject extends BusiObject implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2388248153029890903L;

	public CcodeBusiObject() {

	}

	/**
	 * @param properties
	 */
	public CcodeBusiObject(Map<String, Object> properties) {
		super(properties);

	}

	public CcodeBusiObject(StoreInfo storeinfo) {
		this.storeinfo = storeinfo;
	}

	public String update() {
		StringBuffer updatesql = new StringBuffer();
		updatesql.append("update " + storeinfo.getTablename() + " set ");
		Iterator<String> iterator = this.getPropertyNames().iterator();
		Class c;
		DateTimeFormat activeFormat = DateTimeFormat
				.getFormat("yyyy-M-d H:mm:ss");
		while (iterator.hasNext()) {
			String key = iterator.next();
			// 此字段是虚拟字段，不需要更新
			if (key.equals("addfinacecount") || key.equals("createbname")
					|| key.equals("createdate") || key.equals("createbcode")
					|| key.equals("updatebname") || key.equals("updatedate")
					|| key.equals("updatebcode")) {
				continue;
			}
			if (keyoldvalue.containsKey(key)) {
				if (get(key) != null
						&& !(get(key).equals(keyoldvalue.get(key))))
					updatesql.append("" + key + "='" + get(key) + "',");
			} else {
				/**
				 * 此处专门负责储存的值是modeldata类型诸如 combox
				 */
				if (get(key) instanceof ModelData) {
					Object o = ((ModelData) get(key)).get("code");
					if (o == null) {
						updatesql.append("" + key + "=null,");
					} else {
						updatesql.append("" + key + "='" + o + "',");
					}
				} else {
					if (get(key) == null) {
						updatesql.append("" + key + "=null,");
					} else {
						c = get(key).getClass();
						// Debug.printf("key = "+key +" value = "+ get(key)
						// +" class =" + get(key).getClass(), 4);
						if (c.equals(Integer.class)) {
							updatesql.append("" + key + "=" + get(key) + ",");
						} else if (c.equals(Long.class)) {
							updatesql.append("" + key + "=" + get(key) + ",");
						} else if (c.equals(Float.class)) {
							updatesql.append("" + key + "=" + get(key) + ",");
						} else if (c.equals(Date.class)) {
							if (get(key) != null) {

								updatesql.append("" + key + "='"
										+ activeFormat.format((Date) get(key))
										+ "',");
							}

							else
								updatesql.append("" + key + "=null ,");

						} else if (c.equals(Double.class)) {
							updatesql.append("" + key + "=" + get(key) + ",");
						} else if (c.equals(Boolean.class)) {
							if (((Boolean) get(key)) == true)
								updatesql.append("" + key + "=1,");
							else
								updatesql.append("" + key + "=0,");
						} else {
							updatesql.append("" + key + "='" + get(key) + "',");
						}

						// System.out.println(get(key).getClass());
						// Debug.printf("key="+key +"  class = "+
						// get(key).getClass(), 3);

						// updatesql.append(""+key+"='"+get(key)+"',");
					}
				}
			}

		}

		updatesql.append("updatebcode='" + Env.getEnvbcode() + "',");
		updatesql.append("updatebname='" + Env.getEnvname() + "',");
		updatesql.append("updatedate='" + activeFormat.format(new Date())
				+ "',");
		updatesql = updatesql.replace(updatesql.length() - 1, updatesql
				.length(), "");

		updatesql.append(" where ");
		Iterator<String> keyiterator = keyoldvalue.keySet().iterator();
		while (keyiterator.hasNext()) {
			String key = keyiterator.next();
			Object value = getKeyOldValue(key);

			updatesql
					.append("  " + key + "='" + keyoldvalue.get(key) + "' and");
		}
		// 去掉最后的 and
		updatesql = updatesql.replace(updatesql.length() - 3, updatesql
				.length(), "");

		// updatesql +=" where icode='"+getIcode()+"'";
		// GWT.log(updatesql, null);
		return new String(updatesql);
	}
}
